//
//  ViewController.m
//  PopUpViewDemo
//
//  Created by king on 2021/3/31.
//  Copyright © 2021 Meng. All rights reserved.
//

#import "ViewController.h"
#import "MenuView.h"

#define ScreenSizeWith [UIScreen mainScreen].bounds.size.width
#define ScreenSizeHeight [UIScreen mainScreen].bounds.size.height
#define MengNavHeight     ([[UIApplication sharedApplication] statusBarFrame].size.height + 44)

@interface ViewController ()

@property (nonatomic, strong) MenuView *menuView;
//弹出框是否已显示
@property (nonatomic , assign) BOOL isShow;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenSizeWith - 100, 20, 80, 44)];
    [btn setTitle:@"弹出" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(clcikRightItem) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    __weak __typeof(self) wself = self;
    self.menuView.menuBlock = ^(NSInteger index) {
        /**点击后收回弹框*/
        [wself clcikRightItem];
        /**弹框点击执行事件*/
        switch (index) {
            case 0:
                [btn setTitle:@"添加事件" forState:UIControlStateNormal];
                break;
            case 1:
                [btn setTitle:@"添加好友" forState:UIControlStateNormal];
                break;
            case 2:
                [btn setTitle:@"发布动态" forState:UIControlStateNormal];
                break;
            default:
                break;
        }
    };
}

- (void)clcikRightItem
{
    __weak __typeof(self) wself = self;
    if (self.menuView) {
        if (self.isShow) {
            //收回弹框
            [UIView animateWithDuration:0.25 animations:^{
                wself.menuView.frame = CGRectMake(ScreenSizeWith, MengNavHeight +2, 155, 168);
            } completion:^(BOOL finished) {
                wself.isShow = NO;
            }];
        } else {
            //显示弹框
            [UIView animateWithDuration:0.25 animations:^{
                wself.menuView.frame = CGRectMake(ScreenSizeWith - 157, MengNavHeight +2, 155, 168);
            } completion:^(BOOL finished) {
                wself.isShow = YES;
            }];
        }
    }
}
#pragma mark - Lazy loading
- (MenuView *)menuView
{
    if (!_menuView) {
        _menuView = [[MenuView alloc] initWithFrame:CGRectMake(ScreenSizeWith, MengNavHeight +2, 155, 168) backImage:@"backImage"];
        [self.view addSubview:_menuView];
    }
    return _menuView;
}
@end
